<?php
/**
 * Basic extensions of block
 *
 * @package default
 * @author Daryl Koopersmith
 */

/*function load_default_views($class) {
	include(THEMELIB . '/default_views/' . $class .'.php');
}*/



class Group extends Block {
	var $children;
	
	function __construct($name, $children = null) {
		parent::__construct($name, strtolower(__CLASS__));
		$this->children = $children;
		//load_default_views($this->class);
		add_action( $this->get_action_slug('index'), array($this,'inline') );
		add_action( $this->get_action_slug('admin'), array($this,'admin') );
	}

	function admin() {
		foreach($this->children as $child) {
			$child->run();
		}
	}
	
	function inline() { ?>
		<div id = "<?php echo $this->id; ?>" class="<?php echo $this->class; ?>">
		<?php foreach($this->children as $child) {
			$child->run();
		} ?>
		</div>
	<?php }
}





class Content extends Block {
	function __construct($name) {
		parent::__construct($name, strtolower(__CLASS__));
		//load_default_views($this->class);
		add_action( $this->get_action_slug('index'), array($this,'inline') );
		add_action( $this->get_action_slug('admin'), array($this,'admin') );
	}
	
	function admin() {}
	
	function inline() { ?>
		<div id = "<?php echo $this->id; ?>" class="<?php echo $this->class; ?>">
			<?php include(THEMELIB . '/default_views/' . $this->class . '.php') ?>
		</div>
	<?php }
}





class Sidebar extends Block {
	function __construct($name, $class = null) {
		if (empty($class))
			$class = strtolower(__CLASS__);
		parent::__construct($name, $class);
		
		add_action( $this->get_action_slug('index') . '_before', array($this,'register') );
		add_action( $this->get_action_slug('index'), array($this,'inline') );
		add_action( $this->get_action_slug('index'), array($this,'wrapBefore') , 8);
		add_action( $this->get_action_slug('index'), array($this,'wrapAfter') , 12);
		add_action( $this->get_action_slug('admin'), array($this,'register') );
	}	
	
	function wrapBefore() {
		?><div id = "<?php echo $this->id; ?>" class="<?php echo $this->class; ?> aside"><?php
	}
	
	function wrapAfter() {
		?></div><?php
	}
	
	function inline() {
		//if ( $this->is_sidebar_active($this->id) ) : ?>
			<div id="<?php echo $this->id ?>-widget-area" class="widget-area">
				<ul class="xoxo">
					<?php dynamic_sidebar($this->id); ?>
				</ul>
			</div>
		<?php //endif; 
	} 
	
	// Sidebar template courtesy of Shape.
	// Check for static widgets in widget-ready areas
	function is_sidebar_active( $index ){
	  global $wp_registered_sidebars;

	  $widgetcolums = wp_get_sidebars_widgets();

	  if ($widgetcolums[$index]) return true;

		return false;
	}
	
	function register() {
		register_sidebar( array(
			'name'          => $this->id,
			'id'            => $this->id,
			'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
			'after_widget'  => "</li>",
			'before_title'  => '<h3 class="widget-title">',
			'after_title'   => '</h3>',
			));
	}
}


class Header extends Block {
	function __construct($name) {
		parent::__construct($name, strtolower(__CLASS__));
		add_action( $this->get_action_slug('index'), array($this,'inline') );
		add_action( $this->get_action_slug('admin'), array($this,'admin') );
	}

	function admin() {}	
	
	function inline() { ?>
		<div id = "<?php echo $this->id; ?>" class="<?php echo $this->class; ?>">
			<?php include(THEMELIB . '/default_views/' . $this->class . '.php') ?>
		</div>
	<?php } 
	
}

class Footer extends Sidebar {
	function __construct($name) {
		parent::__construct($name, strtolower(__CLASS__));
		add_action( $this->get_action_slug('index'), array($this,'footer') , 11);
	}
	
	function footer() {
		include(THEMELIB . '/default_views/' . $this->class . '.php');
	} 
	
}

?>